package com.zl.task.controller;

import com.zl.task.config.PassToken;
import com.zl.task.config.UserLoginToken;
import com.zl.task.dto.MechanismDTO;
import com.zl.task.service.IServiceMechanismService;
import com.zl.task.util.resp.RespResult;
import com.zl.task.util.resp.RespUtil;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinlingeng
 * @since 2021-07-23
 */
@RestController
public class ServiceMechanismController {

    @Autowired
    private IServiceMechanismService serviceMechanismService;

    @ApiOperation("机构用户登录")
    @PostMapping("/getMechanism")
    public RespResult<Object> getMechanism(@NonNull String userName, @NonNull String password) {
        try {
            return RespUtil.success(serviceMechanismService.getMechanism(userName,password));
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.error(e.getMessage());
        }
    }

    @UserLoginToken
    @ApiOperation("机构用户信息修改")
    @PostMapping("/updateMechanism")
    public RespResult<Object> updateMechanism(MechanismDTO mechanismDTO) {
        try {
            return RespUtil.success(serviceMechanismService.updateMechanism(mechanismDTO));
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.error(e.getMessage());
        }
    }


    @PassToken(required = true)
    @ApiOperation("密码修改")
    @PostMapping("/updatePassword")
    public RespResult<Object> updatePassword(HttpServletRequest httpServletRequest,Long id,String password,String newPassword) {
        String type = httpServletRequest.getHeader("type");
        try {
            return RespUtil.success(serviceMechanismService.updatePassword(type,id,password,newPassword));
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.error(e.getMessage());
        }
    }


}

